package com.jcgyl.export.service.Impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jcgyl.export.dao.RoleDao;
import com.jcgyl.export.entity.Role;
import com.jcgyl.export.entity.Tbsysuser;
import com.jcgyl.export.mapper.RoleMapper;
import com.jcgyl.export.service.RoleService;
import com.jcgyl.export.util.entity.Result;

@Service
public  class RoleServiceImpl implements RoleService {
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private RoleMapper roleMapper;

	@Override
	public Map<String, Object> findAllRole(String roleName, String parentName, int pageSize, int pageIndex) {
		int rowCount = roleDao.findRowCount(roleName, parentName);
		int startIndex = (pageIndex - 1) * pageSize;
		List<Role> list = roleDao.findAllRole(roleName, parentName, pageSize, startIndex);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", list);
		map.put("totalRows", rowCount);
		return map;
	}

	/** 添加角色 */
	@Override
	public Result addRole(Role role) {
		Tbsysuser user=(Tbsysuser)SecurityUtils.getSubject().getSession().getAttribute("user");
		String roleName = role.getRoleName();
		String parentName= role.getParentName();
		System.out.println("添加角色的姓名 roleName:" + roleName + "parentName:" + parentName);
		try {
		if (role.getParentName().trim() == "" || role.getRoleName().trim() == "") {
				return Result.build(1, "角色的姓名或上级名称不能空!" + "roleName:" + roleName + "parentName:" + parentName);
			}
			int pId=roleDao.findByParentName(parentName).getId();
			role.setpId(pId);
			role.setCreatedUser(user.getUname());
			role.setCreatedTime(new Date());			
			roleMapper.insert(role);
			return Result.ok("添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(1, e.getCause().getMessage());
		}
	}

	/** 修改角色 */
	@Override
	public Result updateRole(Role role) {
		Tbsysuser user=(Tbsysuser)SecurityUtils.getSubject().getSession().getAttribute("user");
		String roleName = role.getRoleName();
		String parentName= role.getParentName();
		System.out.println("修改角色的姓名 roleName:" + roleName + "parentName:" + parentName);
		try {
			if (role.getParentName().trim() == "" || role.getRoleName().trim() == "") {
				return Result.build(1, "角色的姓名或上级名称不能空!" + "roleName:" + roleName + "parentName:" + parentName);
			}
			Role role_update=roleDao.findRoleById(role.getId());
			role_update.setRoleName(roleName);
			role_update.setParentName(parentName);
			role_update.setNote(role.getNote());
			role_update.setModifiedUser(user.getUname());
			role_update.setModifiedTime(new Date());
			roleMapper.updateByPrimaryKey(role_update);
			return Result.ok("修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(1, e.getCause().getMessage());
		}

	}

	/** 删除岗位 */
	@Override
	public Result deleteRole(int id) {
		try {
			roleMapper.deleteByPrimaryKey(id);
			return Result.ok("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(1, e.getCause().getMessage());
		}
	}

	@Override
	public List<Role> findRoleZtree() {
		return roleDao.findRoleZtree();
	}

	@Override
	public List<Role> findRoleName() {
		return roleDao.findRoleName();
	}
}
